﻿using System;
using System.Collections.Generic;

namespace Nova
{
    public class State
    {
        // 状态名称
        public string Name { get; private set; }

        Dictionary<string, object> data = null;
        public const string KReadyToLeave = "ReadyToLeave";
        public T GetData<T>(string k)
        {
            if (data == null || !data.ContainsKey(k))
                return default(T);
            return (T)data[k];
        }
        public void SetData(string k, object v)
        {
            if (data == null)
                data = new Dictionary<string, object>();
            data[k] = v;
        }

        public static bool LeaveAttackingCond(State state)
        {
            return state.GetData<float>(State.KReadyToLeave) <= 0f;
        }

        // 要执行的任务
        public Action<State, float> DoRun { get; private set; }

        // 进入状态时的额外动作
        public Action<State> RunIn { get; private set; }

        // 离开状态时的额外动作
        public Action RunOut { get; private set; }

        // 是否是默认状态
        public bool IsDefault { get; set; }

        public State(string name)
        {
            Name = name;
        }

        public State Run(Action<State, float> doRun)
        {
            DoRun = doRun;
            return this;
        }

        public State OnRunIn(Action<State> runIn)
        {
            RunIn = runIn;
            return this;
        }

        public State OnRunOut(Action runOut)
        {
            RunOut = runOut;
            return this;
        }

        public State AsDefault(bool b = true)
        {
            IsDefault = b;
            return this;
        }
    }
}
